home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / RCS_56.ARJ / RCSDIFF.C < prev    next >
C/C++ Source or Header  |  1992-02-11  |  11KB  |  423 lines

  1. /*
  2.  *                     RCS rcsdiff operation
  3.  */
  4. /*****************************************************************************
  5.  *                       generate difference between RCS revisions
  6.  *****************************************************************************
  7.  */
  8.  
  9. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  10.    Copyright 1990, 1991 by Paul Eggert
  11.    Distributed under license by the Free Software Foundation, Inc.
  12.  
  13. This file is part of RCS.
  14.  
  15. RCS is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2, or (at your option)
  18. any later version.
  19.  
  20. RCS is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with RCS; see the file COPYING.  If not, write to
  27. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  
  29. Report problems and direct all questions to:
  30.  
  31.     rcs-bugs@cs.purdue.edu
  32.  
  33. */
  34.  
  35.  
  36.  
  37.  
  38. /* $Log: rcsdiff.c,v $
  39.  * Revision 5.10  1991/10/07  17:32:46  eggert
  40.  * Remove lint.
  41.  *
  42.  * Revision 5.9  1991/08/19  03:13:55  eggert
  43.  * Add RCSINIT, -r$.  Tune.
  44.  *
  45.  * Revision 5.8  1991/04/21  11:58:21  eggert
  46.  * Add -x, RCSINIT, MS-DOS support.
  47.  *
  48.  * Revision 5.7  1990/12/13  06:54:07  eggert
  49.  * GNU diff 1.15 has -u.
  50.  *
  51.  * Revision 5.6  1990/11/01  05:03:39  eggert
  52.  * Remove unneeded setid check.
  53.  *
  54.  * Revision 5.5  1990/10/04  06:30:19  eggert
  55.  * Accumulate exit status across files.
  56.  *
  57.  * Revision 5.4  1990/09/27  01:31:43  eggert
  58.  * Yield 1, not EXIT_FAILURE, when diffs are found.
  59.  *
  60.  * Revision 5.3  1990/09/11  02:41:11  eggert
  61.  * Simplify -kkvl test.
  62.  *
  63.  * Revision 5.2  1990/09/04  17:07:19  eggert
  64.  * Diff's argv was too small by 1.
  65.  *
  66.  * Revision 5.1  1990/08/29  07:13:55  eggert
  67.  * Add -kkvl.
  68.  *
  69.  * Revision 5.0  1990/08/22  08:12:46  eggert
  70.  * Add -k, -V.  Don't use access().  Add setuid support.
  71.  * Remove compile-time limits; use malloc instead.
  72.  * Don't pass arguments with leading '+' to diff; GNU DIFF treats them as options.
  73.  * Add GNU diff's flags.  Make lock and temp files faster and safer.
  74.  * Ansify and Posixate.
  75.  *
  76.  * Revision 4.6  89/05/01  15:12:27  narten
  77.  * changed copyright header to reflect current distribution rules
  78.  * 
  79.  * Revision 4.5  88/08/09  19:12:41  eggert
  80.  * Use execv(), not system(); yield exit status like diff(1)s; allow cc -R.
  81.  * 
  82.  * Revision 4.4  87/12/18  11:37:46  narten
  83.  * changes Jay Lepreau made in the 4.3 BSD version, to add support for
  84.  * "-i", "-w", and "-t" flags and to permit flags to be bundled together,
  85.  * merged in.
  86.  * 
  87.  * Revision 4.3  87/10/18  10:31:42  narten
  88.  * Updating version numbers. Changes relative to 1.1 actually
  89.  * relative to 4.1
  90.  * 
  91.  * Revision 1.3  87/09/24  13:59:21  narten
  92.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  93.  * warnings)
  94.  * 
  95.  * Revision 1.2  87/03/27  14:22:15  jenkins
  96.  * Port to suns
  97.  * 
  98.  * Revision 4.1  83/05/03  22:13:19  wft
  99.  * Added default branch, option -q, exit status like diff.
  100.  * Added fterror() to replace faterror().
  101.  * 
  102.  * Revision 3.6  83/01/15  17:52:40  wft
  103.  * Expanded mainprogram to handle multiple RCS files.
  104.  *
  105.  * Revision 3.5  83/01/06  09:33:45  wft
  106.  * Fixed passing of -c (context) option to diff.
  107.  *
  108.  * Revision 3.4  82/12/24  15:28:38  wft
  109.  * Added call to catchsig().
  110.  *
  111.  * Revision 3.3  82/12/10  16:08:17  wft
  112.  * Corrected checking of return code from diff; improved error msgs.
  113.  *
  114.  * Revision 3.2  82/12/04  13:20:09  wft
  115.  * replaced getdelta() with gettree(). Changed diagnostics.
  116.  *
  117.  * Revision 3.1  82/11/28  19:25:04  wft
  118.  * Initial revision.
  119.  *
  120.  */
  121. #include "rcsbase.h"
  122.  
  123. #if DIFF_L
  124. static char const *setup_label P((struct buf*,char const*,char const[datesize]));
  125. #endif
  126. static void cleanup P((void));
  127.  
  128. static int exitstatus;
  129. static RILE *workptr;
  130. static struct stat workstat;
  131.  
  132. mainProg(rcsdiffId, "rcsdiff", "$Id: rcsdiff.c,v 5.10 1991/10/07 17:32:46 eggert Exp $")
  133. {
  134.     static char const cmdusage[] =
  135.         "\nrcsdiff usage: rcsdiff [-q] [-rrev1 [-rrev2]] [-Vn] [diff options] file ...";
  136.  
  137.     int  revnums;                 /* counter for revision numbers given */
  138.     char const *rev1, *rev2;    /* revision numbers from command line */
  139.     char const *xrev1, *xrev2;    /* expanded revision numbers */
  140.     char const *expandarg, *lexpandarg, *versionarg;
  141. #if DIFF_L
  142.     static struct buf labelbuf[2];
  143.     int file_labels;
  144.     char const **diff_label1, **diff_label2;
  145.     char date2[datesize];
  146. #endif
  147.     char const *cov[9];
  148.     char const **diffv, **diffp;    /* argv for subsidiary diff */
  149.     char const **pp, *p, *diffvstr;
  150.     struct buf commarg;
  151.     struct buf numericrev;    /* expanded revision number */
  152.     struct hshentries *gendeltas;    /* deltas to be generated */
  153.     struct hshentry * target;
  154.     char *a, *dcp, **newargv;
  155.     register c;
  156.  
  157.     exitstatus = DIFF_SUCCESS;
  158.  
  159.     bufautobegin(&commarg);
  160.     bufautobegin(&numericrev);
  161.     revnums = 0;
  162.     rev1 = rev2 = xrev2 = nil;
  163. #if DIFF_L
  164.     file_labels = 0;
  165. #endif
  166.     expandarg = versionarg = 0;
  167.     suffixes = X_DEFAULT;
  168.  
  169.     /* Room for args + 2 i/o [+ 2 labels] + 1 file + 1 trailing null.  */
  170.     diffp = diffv = tnalloc(char const*, argc + 4 + 2*DIFF_L);
  171.     *diffp++ = nil;
  172.     *diffp++ = nil;
  173.     *diffp++ = DIFF;
  174.  
  175.     argc = getRCSINIT(argc, argv, &newargv);
  176.     argv = newargv;
  177.     while (a = *++argv,  0<--argc && *a++=='-') {
  178.     dcp = a;
  179.     while (c = *a++) switch (c) {
  180.         case 'r':
  181.             switch (++revnums) {
  182.             case 1: rev1=a; break;
  183.             case 2: rev2=a; break;
  184.             default: faterror("too many revision numbers");
  185.             }
  186.             goto option_handled;
  187. #if DIFF_L
  188.         case 'L':
  189.             if (++file_labels == 2)
  190.             faterror("too many -L options");
  191.             /* fall into */
  192. #endif
  193.         case 'C': case 'D': case 'F': case 'I':
  194.             *dcp++ = c;
  195.             if (*a)
  196.             do *dcp++ = *a++;
  197.             while (*a);
  198.             else {
  199.             if (!--argc)
  200.                 faterror("-%c needs following argument%s",
  201.                     c, cmdusage
  202.                 );
  203.             *diffp++ = *argv++;
  204.             }
  205.             break;
  206.         case 'B': case 'H': case 'T':
  207.         case '0': case '1': case '2': case '3': case '4':
  208.         case '5': case '6': case '7': case '8': case '9':
  209.         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  210.         case 'h': case 'i': case 'n': case 'p':
  211.         case 't': case 'u': case 'w':
  212.             *dcp++ = c;
  213.             break;
  214.         case 'q':
  215.             quietflag=true;
  216.             break;
  217.         case 'x':
  218.             suffixes = *argv + 2;
  219.             goto option_handled;
  220.         case 'V':
  221.             versionarg = *argv;
  222.             setRCSversion(versionarg);
  223.             goto option_handled;
  224.         case 'k':
  225.             expandarg = *argv;
  226.             if (0 <= str2expmode(expandarg+2))
  227.             goto option_handled;
  228.             /* fall into */
  229.         default:
  230.             faterror("unknown option: %s%s", *argv, cmdusage);
  231.         };
  232.       option_handled:
  233.     if (dcp != *argv+1) {
  234.         *dcp = 0;
  235.         *diffp++ = *argv;
  236.     }
  237.     } /* end of option processing */
  238.  
  239.     if (argc<1) faterror("no input file%s", cmdusage);
  240.  
  241.     for (pp = diffv+3, c = 0;  pp<diffp;  )
  242.         c += strlen(*pp++) + 1;
  243.     diffvstr = a = tnalloc(char, c + 1);
  244.     for (pp = diffv+3;  pp<diffp;  ) {
  245.         p = *pp++;
  246.         *a++ = ' ';
  247.         while ((*a = *p++))
  248.             a++;
  249.     }
  250.     *a = 0;
  251.  
  252. #if DIFF_L
  253.     diff_label1 = diff_label2 = nil;
  254.     if (file_labels < 2) {
  255.         if (!file_labels)
  256.             diff_label1 = diffp++;
  257.         diff_label2 = diffp++;
  258.     }
  259. #endif
  260.     diffp[2] = nil;
  261.  
  262.     cov[0] = 0;
  263.     cov[2] = CO;
  264.     cov[3] = "-q";
  265.  
  266.     /* now handle all filenames */
  267.     do {
  268.         ffree();
  269.  
  270.         if (pairfilenames(argc, argv, rcsreadopen, true, false)  <=  0)
  271.             continue;
  272.         diagnose("===================================================================\nRCS file: %s\n",RCSfilename);
  273.         if (!rev2) {
  274.         /* Make sure work file is readable, and get its status.  */
  275.         if (!(workptr = Iopen(workfilename,FOPEN_R_WORK,&workstat))) {
  276.             eerror(workfilename);
  277.             continue;
  278.         }
  279.         }
  280.  
  281.  
  282.         gettree(); /* reads in the delta tree */
  283.  
  284.         if (Head==nil) {
  285.             error("no revisions present");
  286.             continue;
  287.         }
  288.         if (revnums==0  ||  !*rev1)
  289.             rev1  =  Dbranch ? Dbranch : Head->num;
  290.  
  291.         if (!fexpandsym(rev1, &numericrev, workptr)) continue;
  292.         if (!(target=genrevs(numericrev.string,(char *)nil,(char *)nil,(char *)nil,&gendeltas))) continue;
  293.         xrev1=target->num;
  294. #if DIFF_L
  295.         if (diff_label1)
  296.         *diff_label1 = setup_label(&labelbuf[0], target->num, target->date);
  297. #endif
  298.  
  299.         lexpandarg = expandarg;
  300.         if (revnums==2) {
  301.             if (!fexpandsym(
  302.                 *rev2 ? rev2  : Dbranch ? Dbranch  : Head->num,
  303.                 &numericrev,
  304.                 workptr
  305.             ))
  306.             continue;
  307.             if (!(target=genrevs(numericrev.string,(char *)nil,(char *)nil,(char *)nil,&gendeltas))) continue;
  308.             xrev2=target->num;
  309.         } else if (
  310.             target->lockedby
  311.         &&    !lexpandarg
  312.         &&    Expand == KEYVAL_EXPAND
  313.         &&    WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
  314.         )
  315.             lexpandarg = "-kkvl";
  316.         Izclose(&workptr);
  317. #if DIFF_L
  318.         if (diff_label2)
  319.         if (revnums == 2)
  320.             *diff_label2 = setup_label(&labelbuf[1], target->num, target->date);
  321.         else {
  322.             time2date(workstat.st_mtime, date2);
  323.             *diff_label2 = setup_label(&labelbuf[1], workfilename, date2);
  324.         }
  325. #endif
  326.  
  327.         diagnose("retrieving revision %s\n", xrev1);
  328.         bufscpy(&commarg, "-p");
  329.         bufscat(&commarg, xrev1);
  330.  
  331.         cov[1] = diffp[0] = maketemp(0);
  332.         pp = &cov[4];
  333.         *pp++ = commarg.string;
  334.         if (lexpandarg)
  335.             *pp++ = lexpandarg;
  336.         if (versionarg)
  337.             *pp++ = versionarg;
  338.         *pp++ = RCSfilename;
  339.         *pp = 0;
  340.  
  341.         if (runv(cov)) {
  342.             error("co failed");
  343.             continue;
  344.         }
  345.         if (!rev2) {
  346.             diffp[1] = workfilename;
  347.             if (workfilename[0] == '+') {
  348.             /* Some diffs have options with leading '+'.  */
  349.             char *dp = ftnalloc(char, strlen(workfilename)+3);
  350.             diffp[1] = dp;
  351.             *dp++ = '.';
  352.             *dp++ = SLASH;
  353.             VOID strcpy(dp, workfilename);
  354.             }
  355.         } else {
  356.             diagnose("retrieving revision %s\n",xrev2);
  357.             bufscpy(&commarg, "-p");
  358.             bufscat(&commarg, xrev2);
  359.             cov[1] = diffp[1] = maketemp(1);
  360.             cov[4] = commarg.string;
  361.             if (runv(cov)) {
  362.                 error("co failed");
  363.                 continue;
  364.             }
  365.         }
  366.         if (!rev2)
  367.             diagnose("diff%s -r%s %s\n", diffvstr, xrev1, workfilename);
  368.         else
  369.             diagnose("diff%s -r%s -r%s\n", diffvstr, xrev1, xrev2);
  370.  
  371.         switch (runv(diffv)) {
  372.             case DIFF_SUCCESS:
  373.                 break;
  374.             case DIFF_FAILURE:
  375.                 if (exitstatus == DIFF_SUCCESS)
  376.                     exitstatus = DIFF_FAILURE;
  377.                 break;
  378.             default:
  379.                 error("diff failed");
  380.         }
  381.     } while (cleanup(),
  382.          ++argv, --argc >=1);
  383.  
  384.  
  385.     tempunlink();
  386.     exitmain(exitstatus);
  387. }
  388.  
  389.     static void
  390. cleanup()
  391. {
  392.     if (nerror) exitstatus = DIFF_TROUBLE;
  393.     Izclose(&finptr);
  394.     Izclose(&workptr);
  395. }
  396.  
  397. #if lint
  398. #    define exiterr rdiffExit
  399. #endif
  400.     exiting void
  401. exiterr()
  402. {
  403.     tempunlink();
  404.     _exit(DIFF_TROUBLE);
  405. }
  406.  
  407. #if DIFF_L
  408.     static char const *
  409. setup_label(b, name, date)
  410.     struct buf *b;
  411.     char const *name;
  412.     char const date[datesize];
  413. {
  414.     char *p;
  415.     size_t l = strlen(name) + 3;
  416.     bufalloc(b, l+datesize);
  417.     p = b->string;
  418.     VOID sprintf(p, "-L%s\t", name);
  419.     VOID date2str(date, p+l);
  420.     return p;
  421. }
  422. #endif
  423.